<?php
	defined( '_JEXEC' ) or die( 'Restricted access' );
	jimport('joomla.application.component.view');
	
	require_once(JPATH_ROOT.DS.'libraries'.DS.'tcpdf'.DS.'tcpdf.php');
	require_once(JPATH_ROOT.DS.'libraries'.DS.'tcpdf'.DS.'config/lang/eng.php');
	
	class HizkimizkiViewHiztegi extends JView
	{
		var $_option = null;
		
		function display($tpl = null)
		{
			global $l;

			$hiztegi = &$this->getModel()->getHiztegi();
			
			// create new PDF document

			$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
			// set document information
			$pdf->setPageOrientation('l'); //l for horizontal p for vertical
			$pdf->SetCreator(PDF_CREATOR);
			$pdf->SetAuthor('Hizkimizki');
			$pdf->SetTitle($hiztegi->_langOri->getName().'-'.$hiztegi->_langDest->getName());
			$pdf->SetSubject('Hizkimizki Dictionary');
			$pdf->SetKeywords('Hizkimizki, Dictionary, '.$hiztegi->_langOri->getName().', '.$hiztegi->_langDest->getName());
			
			// remove default header/footer
			$pdf->setPrintHeader(false);
			$pdf->setPrintFooter(false);
			
			// set default monospaced font
			$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
			
			//set margins
			$pdf->SetMargins(PDF_MARGIN_LEFT, 10, PDF_MARGIN_RIGHT);
			
			//set auto page breaks
			$pdf->SetAutoPageBreak(FALSE, 0);
			
			//set image scale factor
			$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
			
			//set some language-dependent strings
			$pdf->setLanguageArray($l);
			
			// ---------------------------------------------------------
			// set font
			$this->setHiztegiFont($pdf,$hiztegi);
			// add a page
			$pdf->AddPage();
			
			// set some text to print
			
			$html='
				<table height="100%" cellpadding=5>
					<tr>
						<td style="padding-bottom:10px;border-bottom:1px dashed grey;border-right:1px dashed grey;vertical-align:middle">'.$this->printPortada($hiztegi).'</td>
						<td style="padding-bottom:10px;padding-left:5px;border-bottom:1px dashed grey;border-right:1px dashed grey">'.$this->printTraducBlock(0,$hiztegi).'</td>
						<td style="padding-bottom:10px;padding-left:5px;border-bottom:1px dashed grey;border-right:1px dashed grey">'.$this->printTraducBlock(1,$hiztegi).'</td>
						<td style="padding-bottom:10px;padding-left:5px;border-bottom:1px dashed grey;">'.$this->printTraducBlock(2,$hiztegi).'</td>
					</tr>
					<tr>
						<td style="padding-top:10px;padding-left:5px;margin-top:5px;border-right:1px dashed grey">'.$this->printTraducBlock(3,$hiztegi).'</td>
						<td style="padding-top:10px;padding-left:5px;margin-top:5px;border-right:1px dashed grey">'.$this->printTraducBlock(4,$hiztegi).'</td>
						<td style="padding-top:10px;padding-left:5px;margin-top:5px;border-right:1px dashed grey">'.$this->printTraducBlock(5,$hiztegi).'</td>
						<td style="padding-top:10px;padding-left:5px;margin-top:5px;">'.$this->printTraducBlock(6,$hiztegi).'</td>
					</tr>
				</table>';
			
			/*for($i=0;$i<count($hiztegi->_wordsOri);$i++){
				$html=$html.'<tr>';
				$html=$html.'<td>'.$hiztegi->_wordsOri[$i]->_word.'</td>';
				$html=$html.'<td>'.$hiztegi->_wordsDest[$i]->_word.'</td>';
				$html=$html.'</tr>';
			}*/
			/*
			$html='
				<style>
				    div.table {
				        width: 3508px;
						height: 2480px;
						margin: auto;
				    }

				    div.grow {
				        width: 100%;
						height: 50%;
						margin: 0 auto;
				    }

				    div.box {
				    	display: inline-block;
				        width: 24%;
						height: 100%;
				    }

				    div.irow {
				    	width: 100%;
				    	margin: 10px auto;
				    }

				    div.icolr{
				    	width: 40%;
				    	float: right;
				    	margin: 0 auto;
				    }

				    div.icoll{
				    	width: 40%;
				    	float: left;
				    	margin: 0 30px;
				    }

				    .botombordered {
				    	border-bottom: 2px dashed black;
				    }

				    .sidebordered {
				    	border-right: 2px dashed black;
				    }

				</style>';

			$html = $html.'
			<div class="table">
				<div class="grow botombordered">
					<div class="box sidebordered">lelelelele</div>
					<div class="box ">'.$this->printTraducBlock(0,$hiztegi).'</div>
					<div class="box ">'.$this->printTraducBlock(1,$hiztegi).'</div>
					<div class="box ">'.$this->printTraducBlock(2,$hiztegi).'</div>
				</div>
				<div class="grow">
					<div class="box ">'.$this->printTraducBlock(3,$hiztegi).'</div>
					<div class="box ">'.$this->printTraducBlock(4,$hiztegi).'</div>
					<div class="box ">'.$this->printTraducBlock(5,$hiztegi).'</div>
					<div class="box ">'.$this->printTraducBlock(6,$hiztegi).'</div>
				</div>
			</div>';
			*/
			// print a block of text using Write()
			//$pdf->Write($h=0, $txt, $link='', $fill=0, $align='C', $ln=true, $stretch=0, $firstline=false, $firstblock=false, $maxh=0);
			$pdf->writeHTML($html, true, false, true, false, '');
			// ---------------------------------------------------------
			
			//Close and output PDF document
			ob_end_clean();
			$pdf->Output($hiztegi->_langOri->getName().'-'.$hiztegi->_langDest->getName().'.pdf', 'I');
		}
		
		function printPortada($hiztegi){
			$html=
				'<center>
					<img weight="200" height="64" src="'.JPATH_COMPONENT.DS.'images'.DS.'hizkimizki-gris.png'.'"/>
					<br/>
					<br/>
					<br/>
					<br/>
					<br/>
					<br/>
					<font size="20" style="text-align:center;">'.$hiztegi->_langOri->getName().' - '.$hiztegi->_langDest->getName().'</font>
					<br/>
					<br/>
					<font size="8" style="text-align:center;">'.$hiztegi->_langOri->getAuthor().' - '.$hiztegi->_langDest->getAuthor().'</font>
					<br/>
					<br/>
					<br/>
					<br/>
					<br/>
					<br/>
					<br/>
					<br/>
				</center>';
			$html=$html.'<br/><br/>';
			$html=$html.
				'<table width="100%">
					<tr>
						<td>
							<p style="float:left;">
								<br/><br/>
								<br/><br/>
								<br/><br/>
								<font size="10">
									<a href="http://www.hizkimizki.com">www.hizkimizki.com</a>
									<br/>
									<a href="http://www.topagunea.org">www.topagunea.org</a>
								</font>
							</p>
						</td>
						<td>
							<p align="rigth">
								<img weight="100" height="100" src="'.JPATH_COMPONENT.DS.'images'.DS.'topagunea-gris.jpg'.'"/>
							</p>
						</td>
					</tr>
				</table>';
			
			return $html;
		}
		
		function setHiztegiFont($pdf,$hiztegi){
			/*
			if(($hiztegi->_langOri->getId()=="pes"||$hiztegi->_langDest->getId()=="pes") 
				&& ($hiztegi->_langOri->getId()=="cmn"||$hiztegi->_langDest->getId()=="cmn")){
				$pdf->SetFont('ubuntu-r', '', 6);
			}			
			elseif($hiztegi->_langOri->getId()=="pes"||$hiztegi->_langDest->getId()=="pes"){
				$pdf->SetFont('dejavusans', '', 6);
			}
			elseif($hiztegi->_langOri->getId()=="cmn"||$hiztegi->_langDest->getId()=="cmn"){
				$pdf->SetFont('cid0cs', '', 6);
			}
			else{
				$pdf->SetFont('freeserif', '', 6);
			}
			*/
			if($hiztegi->_langOri->getId()=="cmn"||$hiztegi->_langDest->getId()=="cmn"){
				$pdf->SetFont('cid0cs', '', 6);
			}
			else{
				$pdf->SetFont('freeserif', '', 6);
			}
		}
		
		function printTraducBlock($numBlock,$hiztegi){
			$numWords=count($hiztegi->_wordsOri);
			$numWordsSquare=round($numWords/7);
			$start=$numBlock*$numWordsSquare;
			if($numBlock==6){
				$numWordsSquare=$numWordsSquare+($numWords-($numWordsSquare*7));
			}
			$html='<table width="100%" height="100%">';
			$html=$html.'<tr><td></td><td></td></tr>';
			for($i=$start;$i<$start+$numWordsSquare;$i++){
				$html=$html.'<tr>';
				$html=$html.'<td>  '.' '.$hiztegi->_wordsOri[$i]->_word.'</td>';
				$html=$html.'<td>'.$hiztegi->_wordsDest[$i]->_word.'</td>';
				$html=$html.'</tr>';
			}
			
			$html=$html.'</table>';
			/*
			$html = '';
			for($i=$start;$i<$start+$numWordsSquare;$i++){
				$html=$html.'<div class="irow">';
				$html=$html.'<div class="icoll">'.$hiztegi->_wordsOri[$i]->_word.'</div>';
				$html=$html.'<div class="icolr">'.$hiztegi->_wordsDest[$i]->_word.'</div>';
				$html=$html.'</div>';
			}
			*/
			return $html;
		}
		
	}
?>